package com.test.service.impl;

import com.hustcad.plm.pdm.workflow.model.constant.WorkFlowContainerStatusEnum;
import com.hustcad.plm.pdm.workflow.model.vo.AdvancedFlowContainerVO;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowProcessService;
import com.test.entity.DeliverTaskViewDo;
import com.test.mapper.CustomWorkItemMapper;
import com.test.service.CustomWorkItemService;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CustomWorkItemServiceImpl implements CustomWorkItemService {
    @Resource
    private CustomWorkItemMapper customWorkItemMapper;
    @Resource
    private TyplmWorkFlowProcessService typlmWorkFlowProcessService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchTransfer(ResponseResult result, List<AdvancedFlowContainerVO> advancedFlowContainerVOList,
                              BigInteger userId, String remark) {
        try {
            //获取流程Oid
            List<BigInteger> workflowContainerOidList = advancedFlowContainerVOList.stream().map(
                    AdvancedFlowContainerVO::getOid).collect(Collectors.toList());
            log.info("======================workflowContainerOidList==================" + workflowContainerOidList.get(0).toString());
            //根据流程Oid判断状态是否可转办
            List<DeliverTaskViewDo> workFlowInfoByOid = customWorkItemMapper.getWorkFlowInfoByOid(
                    workflowContainerOidList);
            log.info("======================workFlowInfoByOid==================" + workFlowInfoByOid.size());
            //转办流程状态非流程中
            if (workFlowInfoByOid.stream().anyMatch(
                    deliverTaskViewDo -> ObjectUtils.notEqual(deliverTaskViewDo.getStatus(),
                                                              WorkFlowContainerStatusEnum.PROCESSING.getValue()))) {
                result.setSuccess(Boolean.FALSE);
                result.setMessage("转办流程状态非流程中");
                return;
            }
            log.info("======================进入转办==================");
            workFlowInfoByOid.forEach(deliverTaskViewDo -> {
                deliverTaskViewDo.setRemark(remark);
                deliverTaskViewDo.setUserId(userId);
                typlmWorkFlowProcessService.deliverTask(deliverTaskViewDo);
            });
            result.setSuccess(Boolean.TRUE);
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
    }

    @Override
    public void queryUserByName(ResponseResult<List<UserDO>> result, String userName) {
        try {
            List<UserDO> list = customWorkItemMapper.queryUserByName("%" + userName + "%");
            result.setSuccess(Boolean.TRUE);
            result.setData(list);
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
    }
}
